package com.yuqiyu.chapter30.controller;

import com.yuqiyu.chapter30.bean.GoodInfoBean;
import com.yuqiyu.chapter30.bean.GoodTypeBean;
import com.yuqiyu.chapter30.dto.GoodInfoDTO;
import com.yuqiyu.chapter30.jpa.GoodInfoJPA;
import com.yuqiyu.chapter30.jpa.GoodTypeJPA;
import com.yuqiyu.chapter30.mapper.GoodInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试控制器
 * 运行测试
 * 下面我们来创建一个测试的Controller，
 * 用于访问具体请求地址时查询出商品的基本信息以及商品的类型后调用GoodInfoMapper.from(xxx,xxx)方法完成返回GoodInfoDTO实例。
 * Controller代码实现如下所示：
 *
 */
@RestController
public class GoodInfoController
{
    /**
     * 注入商品基本信息jpa
     */
    @Autowired
    private GoodInfoJPA goodInfoJPA;
    /**
     * 注入商品类型jpa
     */
    @Autowired
    private GoodTypeJPA goodTypeJPA;
    /**
     * 注入mapStruct转换Mapper
     */
    @Autowired
    private GoodInfoMapper goodInfoMapper;

    /**
     * 查询商品详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}")
    public GoodInfoDTO detail(@PathVariable("id") Long id)
    {
        //查询商品基本信息
        GoodInfoBean goodInfoBean = goodInfoJPA.findOne(id);
        //查询商品类型基本信息
        GoodTypeBean typeBean = goodTypeJPA.findOne(goodInfoBean.getTypeId());
        //返回转换dto
        return goodInfoMapper.from(goodInfoBean,typeBean);
    }

//Java 变量参数传入方法，修改后是否影响外面的值
    @RequestMapping(value = "/ttt/{id}")
    public String ttt(@PathVariable("id") Long id)
    {
        //查询商品基本信息
        GoodInfoBean goodInfoBean = new GoodInfoBean();
        System.out.println("111============="+goodInfoBean.hashCode());
        GoodInfoBean gg = test(goodInfoBean, id);
        System.out.println("222============="+goodInfoBean.hashCode());
        System.out.println("333=============gg="+gg.hashCode());
        System.out.println("ggg============="+goodInfoBean);
        return "ddd";
    }
    public GoodInfoBean test(GoodInfoBean gg,Long id){
        System.out.println("2-111============="+gg.hashCode());
        gg.setId(222L);//修改后，GoodInfoBean的hashCode就会变动了！！！
        gg = goodInfoJPA.findOne(id);
        gg.setId(333L);
        System.out.println("2-222============="+gg.hashCode());
        System.out.println("2-333============="+gg);
        return gg;
    }

    public static void setValue(StringBuilder str){
        str = new StringBuilder("sss");
    }
    public static void setValue2(StringBuilder str){
        str.append("sss");
    }
    public static void main(String[] args) {

        StringBuilder str = new StringBuilder();
        System.out.println("1==="+str.hashCode());
        setValue(str);
        System.out.println("2==="+str.hashCode());

        System.out.println(str.toString()); //输出空字符串

        setValue2(str);
        System.out.println("3==="+str.hashCode());
        System.out.println(str.toString()); //输出sss

        String a="111";
        System.out.println(a.hashCode());
        a="222";
        System.out.println(a.hashCode());
        a="333";
        System.out.println(a.hashCode());
    }
}